package com.wnx.job.modules.rms.controller;


import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.common.mapstruct.ExperienceMapper;
import com.wnx.job.modules.jms.model.JmsStudent;
import com.wnx.job.modules.jms.service.JmsStudentService;
import com.wnx.job.modules.pms.service.PmsResumeService;
import com.wnx.job.modules.rms.dto.ExperienceParam;
import com.wnx.job.modules.rms.dto.ResumeParam;
import com.wnx.job.modules.rms.model.RmsExperience;
import com.wnx.job.modules.rms.service.RmsExperienceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Slf4j
@Api(tags = "CertificateController",description = "实习经历管理")
@RestController
@RequestMapping("/rms/rmsExperience")
public class RmsExperienceController {

    @Resource
    private JmsStudentService studentService;

    @Resource
    private PmsResumeService resumeService;

    @Resource
    private RmsExperienceService experienceService;

    /**
     * 新增实习经历
     * @param experienceParam
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增实习经历")
    public ResultData<String> save(@RequestBody @Validated ExperienceParam experienceParam, Principal principal){
        log.info("=======系统正在新增实习经历======");
        //获取前端的实习经历对象

        //属性复制
        RmsExperience experience = ExperienceMapper.INSTANCE.toPojo(experienceParam);


        //设置简历ID进实习经历
        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);
        ResumeParam resume = resumeService.findByStudentId(student.getId());
        experience.setResumeId(resume.getId());

        //调用服务层，进行新增实习经历
        experienceService.save(experience);

        log.info("=======系统正在新增实习经历成功！======");
        return ResultData.success(Const.ADD_CERTIFICATE_SUCCESS);


    }

    /**
     * 删除实习经历
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除实习经历")
    public ResultData<String> delete(@PathVariable(name = "id")Long id){
        log.info("========系统正在删除实习经历=====");
        //获取前端的实习经历ID

        //调用服务层，根据ID删除实习经历
        experienceService.removeById(id);

        //返回结果集对象

        log.info("========系统删除实习经历成功！=====");
        return ResultData.success(Const.DELETE_CERTIFICATE_SUCCESS);
    }

    /**
     * 查询实习经历
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("查询实习经历")
    public ResultData<RmsExperience> findById(@PathVariable(name = "id")Long id){
        log.info("========系统正在查询查询实习经历=====");
        //获取前端的实习经历ID
        //调用服务层，根据ID查询实习经历
        RmsExperience experience = experienceService.getById(id);
        log.info("========系统查询实习经历成功！=====");
        return ResultData.success(Const.QUERY_CERTIFICATE_SUCCESS,experience);

    }

    /**
     * 修改实习经历
     * @param experienceParam
     * @param principal
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改实习经历")
    public ResultData<String> update(@Valid @RequestBody ExperienceParam experienceParam, Principal principal){
        log.info("========系统正在修改实习经历！=====");

        //获取前端的实习经历对象

        //属性复制
        RmsExperience experience = ExperienceMapper.INSTANCE.toPojo(experienceParam);


        //设置简历ID进实习经历
        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);
        ResumeParam resume = resumeService.findByStudentId(student.getId());
        experience.setResumeId(resume.getId());

        //调用服务层，进行修改实习经历
        experienceService.updateById(experience);

        log.info("========系统修改实习经历成功！！！=====");
        //返回结果集
        return ResultData.success(Const.UPDATE_CERTIFICATE_SUCCESS);


    }


    /**
     * 查询全部实习经历
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询全部实习经历")
    public ResultData<List<RmsExperience>> findAll(Principal principal){
        log.info("=====系统正在查询全部实习经历=====");
        //通过认证主体，获取学生学号。
        String account = principal.getName();

        //调用服务层，根据学生学号获取学生信息
        JmsStudent student = studentService.findByAccount(account);

        //调用服务层，根据学生ID获取简历
        ResumeParam resume = resumeService.findByStudentId(student.getId());

        //调用服务层进行根据简历ID获取学生实习经历集合
        List<RmsExperience> experienceList = experienceService.findExperienceListByResumeId(resume.getId());

        log.info("=====系统正在查询全部实习经历成功！=====");
        //返回结果集对象
        return ResultData.success(Const.QUERY_CERTIFICATE_SUCCESS,experienceList);
    }
}

